package com.gdut.simple.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gdut.simple.R;
import com.gdut.simple.common.LogUtils;
import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.contract.PostContract;
import com.gdut.simple.data.entity.Post;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.model.IPostModel;
import com.gdut.simple.model.impl.PostModelImpl;
import com.gdut.simple.presenter.PostPresenter;
import com.gdut.simple.view.activity.explore.PostDetailActivity;
import com.gdut.simple.view.adapter.MyPostAdapter;
import com.gdut.simple.view.adapter.base.AbsRecyclerAdapter;
import com.gdut.simple.view.basic.AbsBasicFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class UserPostFragment extends AbsBasicFragment implements PostContract.View {

    private User user;

    @BindView(R.id.id_item_list_view)
    RecyclerView mPostListView;
    @BindView(R.id.id_swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;

    private MyPostAdapter mPostAdapter;
    private List<Post> mPostList;

    private PostContract.Presenter mPresenter;
    private int mCurrentPage = 1;

    public static UserPostFragment newInstance(User user) {
        UserPostFragment userInfoFragment = new UserPostFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable("user", user);
        userInfoFragment.setArguments(bundle);
        return userInfoFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        user = getArguments().getParcelable("user");
        new PostPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user_post, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initRefreshLayout();
        initRecyclerView();
    }

    private void initRefreshLayout() {
        mSwipeRefreshLayout.setColorSchemeColors(ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark),
                ContextCompat.getColor(getActivity(), R.color.colorPrimary),
                ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark));
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                fetchData();
            }
        });
    }

    private void initRecyclerView() {
        mPostListView.setLayoutManager(new LinearLayoutManager(getContext()));
        mPostListView.setItemAnimator(new DefaultItemAnimator());
        mPostList = new ArrayList<>();
        mPostAdapter = new MyPostAdapter(getContext(), mPostList);
        mPostListView.setAdapter(mPostAdapter);
        mPostAdapter.setOnItemClickListener(new AbsRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                Intent intent = new Intent(getActivity(), PostDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("id", mPostList.get(position).getObjectId());
                bundle.putParcelable("post", mPostList.get(position));
                bundle.putString("time", mPostList.get(position).getCreatedAt());
                bundle.putString("profile", mPostList.get(position).getAuthor().getProfile().getUrl());
                bundle.putString("name", mPostList.get(position).getAuthor().getNickName());
                intent.putExtras(bundle);
                startActivity(intent);
            }

            @Override
            public boolean onLongClick(View view, int position) {
                return false;
            }
        });
        mPostListView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItem = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition();
                int totalItemCount = recyclerView.getLayoutManager().getItemCount();
                if (lastVisibleItem >= totalItemCount - 2 && dy > 0) {
                    mPresenter.loadMore(++mCurrentPage);
                }
            }
        });
    }

    @Override
    public void fetchData() {
        mPresenter.refresh();
    }

    @Override
    public User getUser() {
        return user;
    }

    @Override
    public List<Post> getItemList() {
        return mPostList;
    }

    @Override
    public void reset() {
        mCurrentPage = 1;
    }

    @Override
    public void refreshList() {
        if (mPostAdapter != null) {
            mPostAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showRefreshProgress() {
        if (mSwipeRefreshLayout != null) {
            mSwipeRefreshLayout.setRefreshing(true);
        }
    }

    @Override
    public void hideRefreshProgress() {
        if (mSwipeRefreshLayout != null && mSwipeRefreshLayout.isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }

    @Override
    public void setPresenter(@NonNull PostContract.Presenter presenter) {
        this.mPresenter = presenter;
    }
}
